package com.siren.tank.cor;

import com.siren.tank.GameObject;
import com.siren.tank.group.AbstractFactory;
import com.siren.tank.group.BaseTank;
import com.siren.tank.group.Bullet;

/**
 * @author : wangjichao
 * @description:
 * @date : 2020/8/6 17:06
 */
public class BulletTankCollider implements Collider {
    @Override
    public boolean collide(GameObject o1, GameObject o2) {
        if (o1 instanceof Bullet && o2 instanceof BaseTank) {
            if (collideWith((Bullet) o1, (BaseTank) o2)) { return false; }
        } else if (o1 instanceof BaseTank && o2 instanceof Bullet) {
            collide(o2, o1);
        }
        return true;
    }


    public boolean collideWith(Bullet bullet, BaseTank baseTank) {
        if (baseTank.getGroup() == bullet.getGroup()) {
            return false;
        }
        if (isIntersection(baseTank.getX(), baseTank.getY(), BaseTank.WIDTH, BaseTank.HEIGHT,
                            bullet.getX(), bullet.getY(), Bullet.WIDTH, Bullet.HEIGHT)) {
            bullet.setLive(false);
            baseTank.setLive(false);
            AbstractFactory.INSTANCE.createExplode(baseTank.getX(), baseTank.getY());
            return true;
        }
        return false;
    }

}
